// Gallery Game

function stopOrStart() {

	document.gamef.targetShot.value = "no";
	document.gamef.correctTarget.value = "";
	
	if ( document.gamef.gameb.value == "Start Game" ) {
		document.gamef.gameb.value="Stop Game";
		document.gamef.lifeCt.value = "5";
		for (i = 1; i <= 5; i++) {
			setVisibility("lives" + i,"visible");
		}
		getTargets();
	} else {
		parent.gameframe.location.reload(true);
	}
}

function getTargets() {
	var type = [ "english", "korean" ];
	var first = Math.floor(Math.random()*2);
	var second = (first==0)?1:0;
	post(showTargets,"gallery.php","first=" + type[first] + "&second=" + type[second]);
}

function showWord(value) {
	var targetWord = document.getElementById('targetWord');
	targetWord.innerHTML = value;
	setVisibility("targetWord","visible");	
}

function decreaseLife() {
	var lifeCt = parseInt(document.gamef.lifeCt.value);
	if ( lifeCt > 0 ) {
		setVisibility("lives" + lifeCt,"hidden");
	}
	lifeCt--;
	document.gamef.lifeCt.value = "" + lifeCt;
}

function hideTargets() {
	setVisibility("targetWord", "hidden");
	for (i = 1; i <= 4; i++) {
		setVisibility("target" + i,"hidden");
	}
}

function resetTargets() {
	hideTargets();
	if ( document.gamef.targetShot.value == "no" ) {
		decreaseLife();
	}
	if ( parseInt(document.gamef.lifeCt.value) > 0 ) {
		getTargets();
	}
}

function showTarget(id,value) {
	var target = document.getElementById(id);
	target.innerHTML = value;
	var left = Math.round(300*Math.random());
	target.style.marginLeft = left + "px";
	setVisibility(id,"visible");
}

function showTargets() {
	if (xmlHttp.readyState == 4) {
		document.gamef.targetShot.value = "no";
		var values = xmlHttp.responseText.split(";");
		showWord(values[0]);
		var options = values[1].split(",");
		for ( i = 0; i < 4; i++ ) {
			showTarget("target" + (i+1),options[i]);
		}
		document.gamef.correctTarget.value = values[2];
		setTimeout("resetTargets()",5000);
	}
}

function shoot(target) {
	document.gamef.targetShot.value = "yes";
	if ( target.innerHTML != document.gamef.correctTarget.value ) {
		decreaseLife();
		playSound("wrong");
	} else {
		playSound("right");
	}
	setTimeout("setVisibility('" + target.id + "','hidden')",500);
}
